package com.deng.etl.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.deng.etl.common.IEtlInterface;
import com.deng.etl.common.EtlLog;
//制空   t_org_organization_workload_month  床位数
public class ImportICUBedNumber_count extends ImportBase implements IEtlInterface {
	
	public static String COLUMNS=" count(BED_STATE)  as count  ";
	public static String SELECT="select "+COLUMNS+"  from COM_BEDINFO ";
	
	public static String INSERT="insert into t_org_organization_workload_month " +
		"(stat_time,org_operate_organization_id,org_operate_organization_name," +
		"create_time,ip_in_count,ip_out_count," +
		"op_count,cure_count,bed_use_rate,bed_use_count,bed_open_count) " +
		"values(?,'18I1','ICU病区',?,null,null,null,null,null,null,?)";
	
	public static String UPDATE="update t_org_organization_workload_month " +
			"	set bed_open_count=(case when bed_open_count is null then 0 else bed_open_count end)+? where " +
			" stat_time=? and org_operate_organization_id='18I1'";
	
	public static String DELETE="update t_org_organization_workload_month set bed_open_count=null where stat_time>=? and stat_time<?";

	public void insertData(Connection sourceConn,Connection targetConn,String name,String fromDate,String toDate){
		System.out.println("从："+fromDate+"   到："+toDate);
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst=sourceConn.prepareStatement(SELECT);
			System.out.println("-SELECT-------"+SELECT);
			PreparedStatement pst_insert=targetConn.prepareStatement(INSERT);
			PreparedStatement pst_update=targetConn.prepareStatement(UPDATE);
			try {
				pst.setDate(1, getSqlDate(format_s.parse(fromDate)));
				pst.setDate(2, getSqlDate(format_s.parse(toDate)));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ResultSet rs=pst.executeQuery();
			while(rs.next()){
				int count =rs.getInt("count");
//				int year=rs.getInt("year");
//				int month=rs.getInt("month");
//				java.sql.Date insertdate=new java.sql.Date(year-1900,month-1,1);
				java.sql.Date insertdate=new java.sql.Date(
						Integer.valueOf(fromDate.split("-")[0])-1900,
						Integer.valueOf(fromDate.split("-")[1])-1,1);
				boolean flag=checkExist(targetConn, insertdate, "18I1");
				if(flag){
					pst_update.setInt(1, rs.getInt("count"));
					pst_update.setDate(2, insertdate);
					pst_update.executeUpdate();
				}
				else{					
					pst_insert.setDate(1, insertdate);
					java.util.Date date1=new java.util.Date();
					pst_insert.setTimestamp(2, getTimestamp(date1));
					pst_insert.setInt(3, rs.getInt("count"));
					
					pst_insert.executeUpdate();
				}
			}
			rs.close();
			pst.close();
			pst_update.close();
			pst_insert.close();
			//this.finishOneTaskETLLog(targetConn, name, toDate);
				
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public boolean checkExist(Connection conn,java.sql.Date date,String id){
		boolean ret=false;
		try {
			System.out.println("---------checkExist-------"+date.toLocaleString()+";"+id);
			PreparedStatement pst =conn.prepareStatement("select stat_time from t_org_organization_workload_month" +
					" where stat_time=? and org_operate_organization_id=?");
			pst.setDate(1, date);
			pst.setString(2, id);
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
				ret=true;
			}
			rs.close();
			pst.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	
	public void deleteData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		// TODO Auto-generated method stub
		System.out.println("从："+fromDate+"   到："+toDate);
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			PreparedStatement pst_delete=targetConn.prepareStatement(DELETE);
			try {
				pst_delete.setDate(1, getSqlDate(format_s.parse(fromDate)));
				pst_delete.setDate(2, getSqlDate(format_s.parse(toDate)));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			pst_delete.executeUpdate();
			pst_delete.close();
			//EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}
		catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	public void updateData(Connection sourceConn, Connection targetConn,
			String name, String fromDate, String toDate) {
		System.out.println("从："+fromDate+"   到："+toDate);
		try{
			EtlLog.progressOneTaskETLLog(targetConn, name, null);//启动日志
			deleteData(sourceConn, targetConn,name, fromDate, toDate);
			insertData(sourceConn, targetConn,name, fromDate, toDate);
			//EtlLog.finishOneTaskETLLog(targetConn, name, toDate);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
	
	
	
}
